package com.wlstock.funi.MissionInstance;

import android.content.Context;

import com.android.volley.VolleyError;
import com.em.mission.Mission;
import com.em.mission.MissionItem;
import com.em.mission.MissionListener;
import com.em.mission.MissionResultListener;
import com.wlstock.funi.app.FundApplication;
import com.wlstock.funi.constant.SharePrefConstants;
import com.wlstock.funi.request.EventBusConstants;
import com.wlstock.funi.request.NetWorkListener;
import com.wlstock.funi.request.WenGuRequest;
import com.wlstock.funi.utils.GlobalIndexHelper;
import com.wlstock.funi.utils.SharePreferenceUtils;
import com.wlstock.funi.utils.StockUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by admin on 16/8/2.
 *
 * 获取大环境涨跌幅
 */

public class ZDFMission extends MissionItem implements MissionListener, NetWorkListener {

    private static final String ZFHIGHERTHAN3 = "今日涨幅大于3%,上市大于100";
    private static final String DFHIGHERTHAN3 =  "今日跌幅小于-3%,上市大于100";
    private static final String ZFHIGHERTHAN10 = "今日涨停,上市大于100";
    private static final String DFHIGHERTHAN10 = "今日跌停,上市大于100";


    private WenGuRequest wenguReq;
    private String marketStaContent;

    private static ZDFMission instance;

    public static ZDFMission getInstance(Context mContex, String name) {
        if (instance == null) {
            synchronized (ZDFMission.class) {
                if (instance == null) {
                    instance = new ZDFMission(mContex, name);
                }
            }
        }
        return instance;
    }


    private ZDFMission(Context mContext, String name) {
        super(mContext, name);
    }

    @Override
    public void start(MissionResultListener resultListener) {
        this.resultListener = resultListener;
        start(this, resultListener);
    }

    @Override
    public void beforeExecuteListener(Mission mission) {

    }

    @Override
    public void onExecute(Mission mission) {
        retryTime = 0;
        marketStaContent = "";
        zdfReq(ZFHIGHERTHAN3);
    }

    private void zdfReq(String tagName) {
        if (wenguReq == null) {
            wenguReq = new WenGuRequest();
        }
        wenguReq.netWorkListener = this;
        wenguReq.request(tagName, tagName);
    }

    @Override
    public void afterExecute(Mission mission) {

    }

    @Override
    public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {

    }

    @Override
    public void onErrorResponse(VolleyError error, String eventCode) {
        retryTime++;
        if (retryTime <= maxRetryTime){
            if (ZFHIGHERTHAN3.equals(eventCode)){
                zdfReq(ZFHIGHERTHAN3);
            }else if (DFHIGHERTHAN3.equals(eventCode)){
                zdfReq(DFHIGHERTHAN3);
            }else if (ZFHIGHERTHAN10.equals(eventCode)){
                zdfReq(ZFHIGHERTHAN10);
            }else if (DFHIGHERTHAN10.equals(eventCode)){
                zdfReq(DFHIGHERTHAN10);
            }
        }else{
            //重新开始访问
            onExecute(this);
            if (resultListener!=null){
                resultListener.executeFaile(this);
            }
        }

    }

    @Override
    public void onResponse(String response, EventBusConstants eventCode) {

    }

    @Override
    public void onResponse(String response, String eventCode) {
        String count =getWenguCount(response, eventCode);
        saveWenguDataPCT(count, eventCode);

        if (ZFHIGHERTHAN3.equals(eventCode)){
           zdfReq(DFHIGHERTHAN3);
        }else if (DFHIGHERTHAN3.equals(eventCode)){
            zdfReq(ZFHIGHERTHAN10);
        }else if (ZFHIGHERTHAN10.equals(eventCode)){
            zdfReq(DFHIGHERTHAN10);
        }else if (DFHIGHERTHAN10.equals(eventCode)){
            if (resultListener != null){
                JSONObject obj = new JSONObject();
                try {
                    obj.put("content", marketStaContent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                resultListener.executeSuccess(obj,this);
            }
        }
    }

    private String getWenguCount(String response, String eventCode){
        String count = StockUtils.parseWgResponseCount(response);
        eventCode = eventCode.replace(",上市大于100","");
        String speak = eventCode+"有"+count+"家";
        marketStaContent = marketStaContent + speak+"  ";
        FundApplication.getInstance().speak(speak);
        return count;
    }


    private void saveWenguDataPCT(String count, String eventCode) {
        int countInt = -1;
        try {
            countInt = Integer.valueOf(count);
        }catch (Exception e){
            e.printStackTrace();
            return;
        }
        if (ZFHIGHERTHAN3.equals(eventCode)){
            SharePreferenceUtils.setIntSP(FundApplication.getInstance(), SharePrefConstants.COUNT_ZFHIGHERTHAN3,countInt);
            GlobalIndexHelper.getInstance(mContext).set3ZFPCT();
        }else if (DFHIGHERTHAN3.equals(eventCode)){
            SharePreferenceUtils.setIntSP(FundApplication.getInstance(), SharePrefConstants.COUNT_DFHIGHERTHAN3,countInt);
            GlobalIndexHelper.getInstance(mContext).set3ZFPCT();
        }else if (ZFHIGHERTHAN10.equals(eventCode)){
            SharePreferenceUtils.setIntSP(FundApplication.getInstance(), SharePrefConstants.COUNT_ZFHIGHERTHAN10,countInt);
            GlobalIndexHelper.getInstance(mContext).set10ZFPCT();
        }else if (DFHIGHERTHAN10.equals(eventCode)){
            SharePreferenceUtils.setIntSP(FundApplication.getInstance(), SharePrefConstants.COUNT_DFHIGHERTHAN10,countInt);
            GlobalIndexHelper.getInstance(mContext).set10ZFPCT();
        }
    }

}
